// page/pack-service/pages/bespeak-number2/bespeak-number2.js
var toolUtils = require("../../../../utils/tool-utils.js")
var that
var current=0
var num = 30
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dateList: [],
    indicatorDots: false,
    autoplay: false,
    circular:false,
    ckDate: 0,
    current:0,
    list: [{ time: '9:00-10:00', num: 2 }, { time: '10:00-11:00', num: 0 }, { time: '12:00-13:00', num: 1 }, { time: '12:00-13:00', num: 5 }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this
    that.setData({
      dateList: getData(num)
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 预约
   */
  bespeak:function(e){
    toolUtils.pageTo("/page/pack-service/pages/bespeak-signup/bespeak-signup")
  },
  /**
   * 选择日期
   */
  onDate: function (e) {
    var index = e.currentTarget.dataset.index
    that.setData({
      ckDate: index
    })
  },
  /**
   * 日期前移
   */
  leftDate:function(e){
    if (current > 0){
      that.setData({
        current: --current
      })
    }
  },
   /**
   * 日期后移
   */
  rightDate: function (e) {
    if (current < num-5){
      console.log(1)
      that.setData({
        current: ++current
      })
    }
  },
  onDataChange:function(e){
    current = e.detail.current
  }
})
/**
 * 获取日期
 */
function getData(days) {
  var dates = []
  var myDate = new Date()
  var year = myDate.getFullYear()
  var month = myDate.getMonth() + 1
  var day = myDate.getDate()
  var monthDays = getMonthDay(month, year)
  if (days <= monthDays - day+1){
    for (var i = day; i <= days;i++){
      var date = {
        year: year,
        month: month,
        day:i
      }
      dates.push(date)
    }
  }else{
    var d = day
    for (var i = day; i <= days; i++) {
     
      var date = {
        year: year,
        month: month,
        day: d
      }
      dates.push(date)
      //如果大于当月天数则月份加一
      if (d == monthDays) {
        d = 0
        month++
        monthDays = getMonthDay(month, year)
        //如果大于12月则年份加一
        if (month == 13) {
          year++
          d = 0
          month = 1
          monthDays = getMonthDay(month, year)
        }
      }
      d++
    
    }
  }
  return dates;
}
/**
 * 判断是否是闰年
 */
function isLeapYear(year) { return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0); }
/**
 * 获取月份的天数
 */
function getMonthDay(month, year) {
  switch (month) {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
      return 31;
    case 4:
    case 6:
    case 9:
    case 11:
      return 30;
    case 2:
      if (isLeapYear(year)) {
        return 29;
      }
      else {
        return 28;
      }
  }
}